<template>
  <div class="container">
    <el-dialog title="新增员工" @close="btnCancel" :visible="showDialog">
      <el-form
        ref="addEmployee"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="formData.username"
            class="wp50"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="formData.mobile"
            class="wp50"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            class="wp50"
            placeholder="请选择入职时间"
          />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="formData.formOfEmployment"
            class="wp50"
            placeholder="请选择聘用形式"
          >
            <!-- label显示值 value存储值 -->
            <el-option
              v-for="item in EmployeeNum.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="formData.workNumber"
            class="wp50"
            placeholder="请输入工号"
          />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            @focus="getDepartments"
            v-model="formData.departmentName"
            class="wp50"
            placeholder="请选择部门"
          />
          <!-- 树形组件 -->
          <el-tree
            @node-click="selectNode"
            v-loading="loading"
            v-if="showTree"
            :data="treeData"
            :props="defaultProps"
          />
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            class="wp50"
            placeholder="请选择转正时间"
          />
        </el-form-item>
      </el-form>

      <!-- 确定取消按钮 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="btnOk" type="primary" style="margin-right: 10px"
            >确定</el-button
          >
          <el-button @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addEmployee } from "@/api/employees";
import { getDepartments } from "@/api/departments";
// 转化树形结构
import { transListToTreeData } from "@/utils";
import EmployeeNum from "@/api/constant/employees";

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 聘用形式
      EmployeeNum,
      // 表单数据
      formData: {
        user: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      rules: {
        user: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 5, message: "用户姓名为2-5位" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "请输入工号", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
      },
      // 存储部门树形数据
      treeData: [],
      // 表示树形字段的配置信息
      defaultProps: {
        // 指定展示字段为name
        label: "name",
      },
      // 树形显示控件
      showTree: false,
      loading: false,
    };
  },
  // created() {
  //   this.getDepartments();
  // },
  methods: {
    // 选择部门
    selectNode(data) {
      // console.log(arguments);
      this.formData.departmentName = data.name;
      this.showTree = false;
    },
    // 获取部门
    async getDepartments() {
      this.loading = true;
      this.showTree = true;
      const { depts } = await getDepartments();
      this.treeData = transListToTreeData(depts, "");
      this.loading = false;
    },
    async btnOk() {
      try {
        await this.$refs.addEmployee.validate();
        await addEmployee(this.formData);
        this.$message.success("新增员工成功");
        // $parent 当前父组件实例 必须包裹在原生组件中
        // $children 当前子组件集合
        // 更新员工列表
        this.$parent.getEmployeesList && this.$parent.getEmployeesList();
        this.$parent.showDialog = false;
      } catch (e) {
        this.$message.error("操作失败");
        console.log(e);
      }
      this.formData = {
        user: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      };
    },
    btnCancel() {
      this.$refs.addEmployee.resetFields();
      this.$emit("update:showDialog", false);
    },
  },
};
</script>

<style scoped lang='scss'></style>